﻿using ShortLink.Common.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ShortLink.Common.Results
{
    public record JsonData(int Code, object? Data, string? Msg)
    {
        // 成功，不传入数据
        public static JsonData BuildSuccess() => new(0, null, null);

        // 成功，传入数据
        public static JsonData BuildSuccess(object data) => new(0, data, null);

        // 失败，传入描述信息
        public static JsonData BuildError(string msg) => new(-1, null, msg);

        // 自定义状态码和错误信息
        public static JsonData BuildCodeAndMsg(int code, string msg) => new(code, null, msg);

        // 传入枚举，返回信息
        public static JsonData BuildResult(BizCodeEnum codeEnum) => new(codeEnum.GetCode(), null, codeEnum.GetMessage());

        // 获取远程调用数据，反序列化为指定类型
        public T? GetData<T>()
        {
            if (Data is null) return default;
            return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(Data));
        }
    }
    
}
